﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Planer.Model;

namespace Planer.UI.Forms
{
    public partial class TransferMoneyForm : Form
    {
        public TransferMoneyForm()
        {
            InitializeComponent();
            CustomInitializeComponent();
        }

        public TransferMoneyForm(billing bill)
        {
            Bill = bill;
            InitializeComponent();
            CustomInitializeComponent();
        }

        billing Bill = null;

        private void CustomInitializeComponent()
        {
            labelBalance.Text = Bill.balance.ToString("C");

            List<billing> billings = BillingModel.GetInstance().GetBillingListByUser(Program.CurrentUser);
            foreach (billing b in billings)
            {
                if (b.id != Bill.id)
                    listBoxBillings.Items.Add(b);
            }
            if (billings.Count == 0)
            {
                buttonOK.Enabled = false;
                textBoxSumm.Enabled = false;
                return;
            }
            listBoxBillings.SelectedIndex = 0;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            int selected = listBoxBillings.SelectedIndex;
            if (selected < 0) return;

            string summ = textBoxSumm.Text.Trim().Replace('.', ',');
            float money = 0;
            try
            {
                money = Convert.ToSingle(summ);
            }
            catch (Exception ex)
            {
                MessageBoxUtils.ShowErrorBox("Некорректно введена сумма");
                return;
            }
            ModelResult mr = BillingModel.GetInstance().TransferMoney(Bill, (billing)listBoxBillings.SelectedItem, money);
            if (mr.Status == OperationStatusType.ERROR)
            {
                MessageBoxUtils.ShowErrorBox(mr.ShowAllMessages);
                return;
            }
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }
    }
}
